/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.table;

import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import simplemetrosystem.control.EditCommandManager;
import simplemetrosystem.model.Element.MetroEdge;
import simplemetrosystem.model.charlanguage.NameMap;
import simplemetrosystem.view.InformationPane;
import simplemetrosystem.view.table.Exceptions.InformationPanelException;

/**
 *
 * @author Eric Qian
 */
public class EdgeTable extends JTable {

    /**
     * 
     * @param infPane 
     * @param edge
     */
    public EdgeTable(InformationPane infPane, MetroEdge edge) {
        super();
        this.infPane = infPane;
        this.edge = edge;
        edgeModel = new EdgeModel(infPane, edge);
        this.setModel(edgeModel);
        setRowSelectionAllowed(false);
        getTableHeader().setReorderingAllowed(false);
        getTableHeader().setResizingAllowed(false);

        setCell();

        this.setRowHeight(ROW_HEI);
    }

    private void setCell() {
        TableColumn valColumn = columnModel.getColumn(LineModel.COL_VAL);
        valColumn.setCellEditor(new EdgeValCellEditor(infPane, this));
    }

    public MetroEdge getEdge() {
        return edge;
    }
    private InformationPane infPane;
    private MetroEdge edge;
    private EdgeModel edgeModel;
    private static final int ROW_HEI = 20;
}

class EdgeModel extends AbstractTableModel {

    public EdgeModel(InformationPane infPane, MetroEdge edge) {
        super();
        this.infPane = infPane;
        this.edge = edge;
        nameMap = NameMap.getInstance();
        editCommandManager = EditCommandManager.getInstance();
        initArray();
    }

    private void initArray() {
        rowNames = new String[ROW_COUNT];
        rowElems = new String[ROW_COUNT];

        rowNames[ROW_SA_EN] = nameMap.getName("Station A(English)");
        rowElems[ROW_SA_EN] = edge.getStationA().getName();

        rowNames[ROW_SB_EN] = nameMap.getName("Station B(English)");
        rowElems[ROW_SB_EN] = edge.getStationB().getName();

        rowNames[ROW_SA_CN] = nameMap.getName("Station A(Chinese)");
        rowElems[ROW_SA_CN] = edge.getStationA().getChineseName();

        rowNames[ROW_SB_CN] = nameMap.getName("Station B(Chinese)");
        rowElems[ROW_SB_CN] = edge.getStationB().getChineseName();

        rowNames[ROW_LIN_EN] = nameMap.getName("Lines(English)");
        rowElems[ROW_LIN_EN] = edge.getLine().getName();

        rowNames[ROW_LIN_CN] = nameMap.getName("Lines(Chinese)");
        rowElems[ROW_LIN_CN] = edge.getLine().getChineseName();

        rowNames[ROW_SER] = nameMap.getName("Serial Number");
        rowElems[ROW_SER] = "" + edge.getSerialNumber();

        rowNames[ROW_STA] = nameMap.getName("Edge Status");
        rowElems[ROW_STA] = nameMap.getName(edge.getEdgeStatus());

        rowNames[ROW_DIS] = nameMap.getName("Distance(km)");
        rowElems[ROW_DIS] = "" + edge.getDistance();

        rowNames[ROW_TIM] = nameMap.getName("Time(min)");
        rowElems[ROW_TIM] = "" + edge.getTime();
    }

    @Override
    public Class getColumnClass(int column) {
        return String.class;
    }

    public int getRowCount() {
        return ROW_COUNT;
    }

    @Override
    public String getColumnName(int column) {
        if (column == COL_PRO) {
            return nameMap.getName("Property");
        } else if (column == COL_VAL) {
            return nameMap.getName("Value");
        } else {
            return "???";
        }
    }

    public int getColumnCount() {
        return COL_COUNT;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (columnIndex == COL_VAL && rowIndex != ROW_STA) {
            return true;
        }
        return false;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (rowIndex == ROW_SER) {
            editCommandManager.EditEdge(edge, edge.getLine(),
                    Integer.parseInt((String) aValue), edge.getDistance(), edge.getTime());
        } else if (rowIndex == ROW_DIS) {
            editCommandManager.EditEdge(edge, edge.getLine(),
                    edge.getSerialNumber(), Double.parseDouble((String) aValue),
                    edge.getTime());
        } else if (rowIndex == ROW_TIM) {
            editCommandManager.EditEdge(edge, edge.getLine(),
                    edge.getSerialNumber(), edge.getDistance(),
                    Double.parseDouble((String) aValue));
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            return rowNames[rowIndex];
        } else if (columnIndex == 1) {
            return rowElems[rowIndex];
        } else {
            return "???";
        }

    }
    private InformationPane infPane;
    private NameMap nameMap;
    private EditCommandManager editCommandManager;
    private MetroEdge edge;
    private String[] rowNames;
    private String[] rowElems;
    public static final int ROW_COUNT = 10;
    public static final int COL_PRO = 0;
    public static final int COL_VAL = 1;
    public static final int COL_COUNT = 2;
    public static final int ROW_SA_EN = 0;
    public static final int ROW_SB_EN = 1;
    public static final int ROW_SA_CN = 2;
    public static final int ROW_SB_CN = 3;
    public static final int ROW_LIN_EN = 4;
    public static final int ROW_LIN_CN = 5;
    public static final int ROW_SER = 6;
    public static final int ROW_STA = 7;
    public static final int ROW_DIS = 8;
    public static final int ROW_TIM = 9;
}

class EdgeValCellEditor extends DefaultCellEditor {

    public EdgeValCellEditor(InformationPane infPane, EdgeTable table) {
        super(new JTextField());
        this.infPane = infPane;
        this.table = table;
        this.model = (EdgeModel) table.getModel();
    }

    @Override
    public Object getCellEditorValue() {
        return super.getCellEditorValue();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        try {
            if (row == EdgeModel.ROW_SA_EN || row == EdgeModel.ROW_SA_CN) {
                infPane.setFocus(InformationPane.STATION, this.table.getEdge().getStationA());
                return null;
            } else if (row == EdgeModel.ROW_SB_EN || row == EdgeModel.ROW_SB_CN) {
                infPane.setFocus(InformationPane.STATION, this.table.getEdge().getStationB());
                return null;
            } else if (row == EdgeModel.ROW_LIN_EN || row == EdgeModel.ROW_LIN_CN) {
                infPane.setFocus(InformationPane.LINE, this.table.getEdge().getLine());
                return null;
            } else {
                return super.getTableCellEditorComponent(table, value, isSelected, row, column);
            }
        } catch (InformationPanelException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    private InformationPane infPane;
    private EdgeTable table;
    private EdgeModel model;
}
